package com.foreveross.taskservice.application.impl.task.mq;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import com.foreveross.taskservice.application.task.mq.IRollbackTaskFromMQApplication;
import com.foreveross.taskservice.common.model.AbstractTaskModel;
@Service
public class RollbackTaskFromMQApplicationImpl   implements  IRollbackTaskFromMQApplication{
	private static Logger log=LoggerFactory.getLogger(RollbackTaskFromMQApplicationImpl.class);
	@Resource(name="reciveJmsTemplate")
	private JmsTemplate jmsTemplate;
	@Override
	public AbstractTaskModel roll(javax.jms.Destination destination) throws Exception {
		List<AbstractTaskModel> tasks=rolls(destination,1);
		return tasks==null||tasks.isEmpty()?null:tasks.get(0);
	}

	@Override
	public List<AbstractTaskModel> rolls(javax.jms.Destination destination,int maxSize) throws Exception {
		List<AbstractTaskModel> tasks=new ArrayList<AbstractTaskModel>();
		try{
			while(maxSize-->0){
				AbstractTaskModel model=(AbstractTaskModel) jmsTemplate.receiveAndConvert(destination);
				if(model!=null){
					tasks.add(model);
				}else{
					break;
				}
			}
		}catch(Exception e){
			log.error(ExceptionUtils.getFullStackTrace(e));
		}
		return tasks;
	}

}
